package base.stream.piped;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class PipedInputOutputStreamDemo {
    public static void main(String[] args) throws IOException {
        final PipedOutputStream out = new PipedOutputStream();
        final PipedInputStream in =new PipedInputStream(out);

        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    out.write("Hello , Piped!".getBytes());
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });


        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    int data = in.read();
                    while (data != -1) {
                        System.out.print((char) data);
                        data = in.read();
                    }
                    System.out.println("");
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        });


        thread1.start();
        thread2.start();
    }
}
